/***     
 * 查找顺子
 */
var _ = require('../Underscore');
var FindCardTool = require('./FindCardTool');
var CardTool = require('../CardTool');
var FindShunZiTool = module.exports = {};

/***
 * shunZiNumber:顺子个数
 * return {
 *  isFind : false,
 *  findGroupMore:[{shunzi:[],otherGroup:[]}] 或者 _cardInfoList
 * }
 */
FindShunZiTool.getMoreResult = function(_cardInfoList, shunZiNumber) {
    var resultInfo = this.getResult(_cardInfoList, shunZiNumber);
    if (resultInfo.isFind) {
        var result = {
            isFind: true,
            findGroupMore: []
        }
        var tmpFindGroup = resultInfo.findGroup;
        _.each(tmpFindGroup, function(oneGroupCardInfo) {
            // console.error("oneGroupCardInfo:", JSON.stringify(oneGroupCardInfo))

            var otherGroup = _.difference(_cardInfoList, oneGroupCardInfo);
            // console.error("otherGroup:", JSON.stringify(otherGroup))
            // console.error('找完顺子之后还剩牌数量：', _.size(otherGroup));

            result.findGroupMore.push({
                findGroup: oneGroupCardInfo,
                otherGroup: otherGroup
            });
        }.bind(this));

        CardTool.LogResult(result, ("查找" + shunZiNumber + "连的顺子"))
        return result;
    }
    return resultInfo;
};

/***
 * shunZiNumber:顺子个数
 */
FindShunZiTool.getResult = function(_cardInfoList, shunZiNumber) {
    var resultInfo = this.check(_cardInfoList, shunZiNumber);
    if (resultInfo.isFind) {
        // var findGroup = resultInfo.findGroup;
        // console.error('findGroup:', JSON.stringify(findGroup));
        return resultInfo;
    }
    return resultInfo;
};

FindShunZiTool.check = function(_cardInfoList, shunZiNumber) {
    _cardInfoList = _.sortBy(_cardInfoList, 'value');
    // console.error('排序之后:%s %s', JSON.stringify(
    //         _.map(_cardInfoList, function(cardInfo) {
    //             return cardInfo.value;
    //         })),
    //     JSON.stringify(_cardInfoList));

    var size = _.size(_cardInfoList);

    var result = {
        isFind: false,
        findGroup: [],
        otherGroup: _cardInfoList
    };
    //数组数量都不够
    if (size < shunZiNumber) {
        // console.error("牌数量未达到%d个不可能形成%d连的顺子", shunZiNumber, shunZiNumber);
        return result;
    }

    var fristCardInfo = _cardInfoList[0];
    var lastCardInfo = _cardInfoList[size - 1];

    var tmpValue = Math.abs(lastCardInfo.value - fristCardInfo.value);
    if (tmpValue < (shunZiNumber - 1)) {
        console.error('最大值减去最小值等于%d不可能形成%d连的顺子', tmpValue, shunZiNumber);
        return result;
    }
    //下面是可能存在的逻辑

    //可能形成的组数
    var canGroupNumber = (size - shunZiNumber) + 1;

    //可以组合的情况
    var canGroupList = [];
    var oldFindCardId = null;
    for (var i = 0; i < canGroupNumber; ++i) {
        var beginIdxList = []; //顺子的开始位置
        var findNextCard = _cardInfoList[i].value;

        /***避免重复计算 */
        if (null != oldFindCardId) {
            if (oldFindCardId == findNextCard) {
                continue;
            }
        }
        oldFindCardId = findNextCard;

        // console.error('查找的第一张牌：', findNextCard);
        var _tmpFind = true;
        var findGroup = [_cardInfoList[i]];
        for (var j = 1; j < shunZiNumber; ++j) {
            findNextCard += 1;
            var _tmpResult = FindCardTool.getResult(_cardInfoList, function(_cardInfo) {
                return findNextCard == _cardInfo.value;
            }, function(_cardInfo) {
                return findNextCard != _cardInfo.value;
            });
            if (!_tmpResult.isFind) {
                _tmpFind = false;
                break;
            }
            findGroup.push(_tmpResult.findGroup[0]);
        }
        if (_tmpFind) {
            // console.error('findGroup', JSON.stringify(findGroup));
            canGroupList.push(findGroup);
            console.error('找到%d到%d的顺子', (findNextCard - shunZiNumber + 1), findNextCard);
        }
    }
    return {
        isFind: _.size(canGroupList) > 0,
        findGroup: canGroupList, //
        otherGroup: _cardInfoList
    };
};